package com.example.lanou.mynews.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lanou.mynews.R;
import com.example.lanou.mynews.application.MyApplication;
import com.example.lanou.mynews.face.GridViewAttach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/15.
 */
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.MyHolder> implements GridViewAttach {
    private ArrayList<String> titles;
    private ArrayList<String> data;
    private int pos[] = new int[1];
    private Map<Integer, int[][]> itemDetail;
    private GridViewAttach gridOff;

    public GridViewAdapter(ArrayList<String> data) {
        this.data = data;
        titles = new ArrayList<>();
        itemDetail = new HashMap<>();

    }

    public void setGridOff(GridViewAttach gridOff) {
        this.gridOff = gridOff;
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder holder = new MyHolder(LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_gridview, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        pos[0] = position;
        holder.textView.setText(data.get(position));
        savePosition(holder.itemView);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                gridOff.addData(data.get(pos[0]));
                delData(data.get(pos[0]));
                notifyDataSetChanged();
                return true;
            }
        });
    }

    public void delData(String title) {
        data.remove(pos[0]);
        notifyDataSetChanged();
    }

    public void addData(String title) {
        data.add(title);
        notifyDataSetChanged();
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    //保存item位置信息，
    public void savePosition(View view) {
        Map<Integer, int[][]> itemdata = new HashMap<>();
        int a[][] = {{(int) view.getX(), (int) view.getY()}};
        itemdata.put(pos[0], a);

    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MyHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_grid_tv);
        }
    }

}
